package com.zmn.oms.third.pinduoduo;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoResponse;
import com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoSendRequest;
import com.zmn.oms.third.pinduoduo.util.PinDuoDuoUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2021/01/05 17:19
 */
@Slf4j
public final class PinDuoDuoApiInvoker {

    /**
     * 发送消息给拼多多服务
     * @param pathRequest
     * @param pinDuoDuoSendRequest
     * @return com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoResponse
     * @author wangxiaokun
     */
    public static PinDuoDuoResponse doRequest(String pathRequest, PinDuoDuoSendRequest pinDuoDuoSendRequest) {
        // 参数判断
        if (Objects.isNull(pinDuoDuoSendRequest)) {
            return null;
        }

        int sendTimes = 0;
        PinDuoDuoResponse responseRequest = null;

        // 生成签名
        String signRequest = PinDuoDuoUtil.makeRequestSign(pinDuoDuoSendRequest);
        if (Objects.isNull(signRequest) || signRequest.isEmpty()) {
            log.info("【{}-API】 生成签名失败", PinDuoDuoConsts.CHANNEL_NAME);
            return null;
        }

        Map<String, String> map = new HashMap<>(8);
        map.put("type", pinDuoDuoSendRequest.getType());
        map.put("client_id", pinDuoDuoSendRequest.getClient_id());
        map.put("timestamp", pinDuoDuoSendRequest.getTimestamp().toString());
        map.put("partner_id", pinDuoDuoSendRequest.getPartner_id());
        map.put("target_client_id", pinDuoDuoSendRequest.getTarget_client_id());
        map.put("request_id", pinDuoDuoSendRequest.getRequest_id());
        map.put("sign", signRequest);
        map.put("request", pinDuoDuoSendRequest.getRequest());

        // 是否重试
        while (sendTimes < 2) {

            responseRequest = PinDuoDuoUtil.sendRequestToPinDuoDuo(pathRequest, map);
            if (Objects.isNull(responseRequest)) {
                return null;
            }

            // 返回值在100-199之间重试一次
            if (responseRequest.getErrorCode() < 100 || responseRequest.getErrorCode() > 199) {
                return responseRequest;
            }

            sendTimes++;
        }

        return responseRequest;
    }

    /**
     * 发送消息给拼多多服务
     * @param pathRequest
     * @param paramJson
     * @return com.zmn.oms.third.pinduoduo.dto.api.PinDuoDuoResponse
     * @author wangxiaokun
     */
    public static PinDuoDuoResponse doRequest(String pathRequest, String paramJson) {

        // 判断参数是否正确
        if (Objects.isNull(pathRequest) || pathRequest.isEmpty() || Objects.isNull(paramJson) || paramJson.isEmpty()) {
            return null;
        }

        PinDuoDuoSendRequest pinDuoDuoSendRequest = new PinDuoDuoSendRequest();

        // 判断是否核销码
        if (paramJson.contains("verifyCode")){
            pinDuoDuoSendRequest.setType(PinDuoDuoConsts.PDD_SERVICE_AFTERSALES_VERIFY_CHECK_FALG);
        }

        // 判断是否签约结果回传
        if (paramJson.contains("verifyStatus")){
            pinDuoDuoSendRequest.setType(PinDuoDuoConsts.PDD_SERVICE_AFTERSALES_SIGN_NOTIFY_FLAG);
        }

        // 判断是否服务实操回告
        if (paramJson.contains("bizStatus")){
            pinDuoDuoSendRequest.setType(PinDuoDuoConsts.PDD_SERVICE_AFTERSALES_BIZACTION_NOTYFY_FALG);
        }

        pinDuoDuoSendRequest.setClient_id(PinDuoDuoConsts.PDD_CLIENT_ID);
        pinDuoDuoSendRequest.setTimestamp(DateUtil.getNowTimestamp());
        pinDuoDuoSendRequest.setPartner_id(PinDuoDuoConsts.PDD_PARTNER_ID);
        pinDuoDuoSendRequest.setTarget_client_id(PinDuoDuoConsts.PDD_TARGET_CLIENT_ID);
        pinDuoDuoSendRequest.setRequest_id("zmn_pdd_" + DateUtil.getNowTimestamp());
        pinDuoDuoSendRequest.setRequest(paramJson);

        // 发送请求
        PinDuoDuoResponse response = doRequest(pathRequest, pinDuoDuoSendRequest);
        if (Objects.isNull(response)) {
            log.info("【{}-API】 发送请求失败", PinDuoDuoConsts.CHANNEL_NAME);
            return null;
        }

        return response;
    }

    /**
     * 私有构造器，防止外部示例化
     */
    private PinDuoDuoApiInvoker() {}
}
